import { director, error, log, sys } from "cc";
import { zc } from "./ZC";
import { HttpReturn } from "./ZCHttpRequest";
import { EventManager } from "../EventManager";
import { GameData } from "../GameData";
import { UIManager } from "../UIManager";


export class HttpClient {
    private httpTryCount = 10;  
    private httpTryNum = 0;     
    private static instance:HttpClient = null;
    private isInit:boolean = true;

    private isShowTip:boolean = true;

    private rankTime:number[] = [0,0,0,0];

    private argc:{ [key:string]:any } = {};

    public static getInstance():HttpClient {
        if(this.instance == null) {
            this.instance = new HttpClient();
        }

        return this.instance;
    }

    private constructor() {
        if ((window as any).user_token) {
            //zc.http.addHeader("token",(window as any).user_token);

        }
        zc.http.addHeader("Content-Type", "application/json; charset=utf-8");

        //zc.http.addHeader("Authorization","eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiIxIiwiaWF0IjoiMTcyMDUwNjc1MyIsIm5iZiI6IjE3MjA1MDY3NTMiLCJleHAiOiIxNzIwNTEzOTUzIiwiaXNzIjoidm9sLmNvcmUub3duZXIiLCJhdWQiOiJ2b2wuY29yZSIsIm9wZW5JZCI6IjU1NSIsInd4TmlrZU5hbWUiOiLmnb7mnb4xIn0.UdRs6PekPYMMkgXnTz8K1dAQtRkZQ6bDaNGW3-VJTrI");
    }

    //初始化游戏配置
    sendInitCfgs(){
        this.sendGetShopCfg();
        this.sendGetrewardConfigList();
        this.sendGetAiCfg();
        this.sendGetLinkList();
    }

    /**请求登录 */
    sendLogin(phone:string = "111117", pass:string = "11", success:Function = null){
        let params = {
            "mobile":phone,
            "newPassword":pass, 
        }
        this.argc.sendLoginCallBack = success;
        zc.http.post("/user/smsLogin", this.onLogin.bind(this), params);
    }

    onLogin(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (res.res.data){
                zc.http.addHeader("token",res.res.data.token);
                this.sendGetuserinfo(()=>{
                    HttpClient.getInstance().sendInitCfgs();
                    HttpClient.getInstance().sendGetShopList()
                });
               
                if (this.argc.sendLoginCallBack){
                    this.argc.sendLoginCallBack(res.res);
                    this.argc.sendLoginCallBack = null;
                }
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

     /**请求验证吗 */
    sendGetCode(phone:string = "111117", success:Function = null){
        let params = {
            "mobile":phone, 
        }
        this.argc.sendGetCodeCallBack = success;
        zc.http.post("/user/sendSmsCode", this.onsendGetCode.bind(this), params);
    }

    onsendGetCode(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendGetCodeCallBack){
                    this.argc.sendGetCodeCallBack(res.res);
                    this.argc.sendGetCodeCallBack = null;
                }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**请求注册 */
    sendRegis(phone:string = "111117",code:string, pass:string, name:string = null, IDCard:string = null, invitCode:string = "000000", success:Function = null){
        let params = { 
            "mobile": phone, 
            "code": code, 
            "newPassword": pass, 
            "confirmPassword": pass, 
            "name": name, 
            "idCardNumber": IDCard, 
            "invitationCode": invitCode,
        }
        this.argc.sendRegisCallBack = success;
        zc.http.post("/user/smsRegister", this.onsendRegis.bind(this), params);
    }

    onsendRegis(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendRegisCallBack){
                this.argc.sendRegisCallBack(res.res);
                this.argc.sendRegisCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取玩家商铺列表*/
    sendGetShopList(success:Function = null){
        let params = {
            "uid":GameData.getIns().userData.id,
        }
        zc.http.post("/shop/list", this.onsendGetShopList.bind(this), params);
    }

    onsendGetShopList(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (res.res.data){
                GameData.shopDataList = res.res.data.shopList;
                this.refreshView();
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取用户信息*/
    sendGetuserinfo(success:Function = null){
        let params = {
        }
        this.argc.sendGetuserinfoCallBack = success;
        zc.http.post("/user/userInfo", this.onsendGetuserinfo.bind(this), params);
    }

    onsendGetuserinfo(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (res.res.data){
               GameData.getIns().userData = res.res.data;
                if (this.argc.sendGetuserinfoCallBack){
                    this.argc.sendGetuserinfoCallBack(res.res);
                    this.argc.sendGetuserinfoCallBack = null;
                }
                this.refreshView();
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

     /**修改昵称 */
    sendChangeName(username:string, sign:string, avatar:string,success:Function = null){
        let params = {
            "username": username,
            "avatar": avatar,
            "introduce": sign
        }
        this.argc.sendChangeNameCallBack = success;
        zc.http.post("/user/userInfoEdit", this.onsendChangeName.bind(this), params);
    }

    onsendChangeName(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendChangeNameCallBack){
                this.argc.sendChangeNameCallBack(res.res);
                this.argc.sendChangeNameCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取排行榜信息 */
    sendGetUserRank(success:Function = null){
        let params = { 

        }
        this.argc.sendGetUserRankCallBack = success;
        zc.http.post("/user/userRank", this.onsendGetUserRank.bind(this), params);
    }

    onsendGetUserRank(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendGetUserRankCallBack){
                this.argc.sendGetUserRankCallBack(res.res);
                this.argc.sendGetUserRankCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**请求开店 */
    sendOpenShop(shopLv:number, success:Function = null){
        let params = { 
            "uid": GameData.getIns().userData.id, 
            "shopLv": shopLv,
        }
        this.argc.sendOpenShowCallBack = success;
        zc.http.post("/shop/open", this.onsendOpenShop.bind(this), params);
    }

    onsendOpenShop(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendOpenShowCallBack){
                this.argc.sendOpenShowCallBack(res.res);
                this.argc.sendOpenShowCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**请求升级商铺 */
    sendUpShop( shopLv:number,shopId:number, success:Function = null){
        let params = { 
            "uid": GameData.getIns().userData.id, 
            "shopLv": shopLv,
            "shopId": 1,
        }
        this.argc.sendUpShopCallBack = success;
        zc.http.post("/shop/upGrade", this.onsendUpShop.bind(this), params);
    }

    onsendUpShop(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendUpShopCallBack){
                this.argc.sendUpShopCallBack(res.res);
                this.argc.sendUpShopCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**请求商铺补货 */
    sendAddGoods(shopId:number, success:Function = null){
        let params = { 
            "uid": GameData.getIns().userData.id, 
            "shopId": shopId,
        }
        this.argc.sendAddGoodsCallBack = success;
        zc.http.post("/shop/addGoods", this.onsendAddGoods.bind(this), params);
    }

    onsendAddGoods(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendAddGoodsCallBack){
                this.argc.sendAddGoodsCallBack(res.res);
                this.argc.sendAddGoodsCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**修改密码 */
    sendResetPass(mobile:string, code:string, password:string, confirmPassword:string, success:Function = null){
        let params = { 
            "mobile": mobile, 
            "code": code,
            "password": password,
            "confirmPassword": confirmPassword,
        }
        this.argc.sendChangePassCallBack = success;
        zc.http.post("/user/updatePwd", this.onsendAddGoods.bind(this), params);
    }

    onsendChangePass(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendChangePassCallBack){
                this.argc.sendChangePassCallBack(res.res);
                this.argc.sendChangePassCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**开启智能管家*/
    sendActivityAI(success:Function = null){
        let params = { 
            "uid": GameData.getIns().userData.id, 
        }
        this.argc.sendActivityAICallBack = success;
        zc.http.post("/user/activeAI", this.onsendActivityAI.bind(this), params);
    }

    onsendActivityAI(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendActivityAICallBack){
                this.argc.sendActivityAICallBack(res.res);
                this.argc.sendActivityAICallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取轮播列表*/
    sendGetLinkList(uid:number=0,success:Function = null){
        let params = {  
        }
        this.argc.sendGetLinkListCallBack = success;
        zc.http.post("/link/list", this.onsendGetLinkList.bind(this), params);
    }

    onsendGetLinkList(res:HttpReturn){
        if (res.res && res.res.code == 200){
            GameData.linkCfgs = res.res.data;
            EventManager.eventTarget.emit(EventManager.REFRESH_LINKCFGS);
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取任务列表 0新手，1每日，2成就*/
    sendGetTaskList(type:number,success:Function = null){
        let params = {  
            "uid":GameData.getIns().userData.id,
            "type":type,
        }
        this.argc.sendGetTaskListCallBack = success;
        zc.http.post("/task/list", this.onsendGetTaskList.bind(this), params);
    }

    onsendGetTaskList(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendGetTaskListCallBack){
                this.argc.sendGetTaskListCallBack(res.res);
                this.argc.sendGetTaskListCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取任务奖励*/
    sendGetTaskReward(taskId:number,success:Function = null){
        let params = {  
            "uid":GameData.getIns().userData.id,
            "taskId":taskId,
        }
        this.argc.sendGetTaskRewardCallBack = success;
        zc.http.post("/task/claim", this.onsendGetTaskList.bind(this), params);
    }

    onsendGetTaskReward(res:HttpReturn){
        if (res.res && res.res.code == 200){
            EventManager.eventTarget.emit(EventManager.REFRESH_TASKCFGS);
            this.sendGetuserinfo();
            if (this.argc.sendGetTaskRewardCallBack){
                this.argc.sendGetTaskRewardCallBack(res.res);
                this.argc.sendGetTaskRewardCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取店铺配置表*/
    sendGetShopCfg(success:Function = null){
        let params = {  
        }
        // this.argc.sendGetTaskRewardCallBack = success;
        zc.http.post("/shop/config/list", this.onsendGetShopCfg.bind(this), params);
    }

    onsendGetShopCfg(res:HttpReturn){
        if (res.res && res.res.code == 200){
            GameData.shopCfgs = res.res.data;
            this.refreshView();
            // if (this.argc.sendGetTaskRewardCallBack){
            //     this.argc.sendGetTaskRewardCallBack(res.res);
            //     this.argc.sendGetTaskRewardCallBack = null;
            // }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取邀请奖励配置表*/
    sendGetrewardConfigList(success:Function = null){
        let params = {  
        }
        // this.argc.sendGetTaskRewardCallBack = success;
        zc.http.post("/rewardConfig/list", this.onsendGetrewardConfigList.bind(this), params);
    }

    onsendGetrewardConfigList(res:HttpReturn){
        if (res.res && res.res.code == 200){
            GameData.inviteRewardCfgList = res.res.data;
            // if (this.argc.sendGetTaskRewardCallBack){
            //     this.argc.sendGetTaskRewardCallBack(res.res);
            //     this.argc.sendGetTaskRewardCallBack = null;
            // }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取AI配置表*/
    sendGetAiCfg(success:Function = null){
        let params = {  
        }
        // this.argc.sendGetTaskRewardCallBack = success;
        zc.http.post("/shop/ai/list", this.onsendGetAiCfg.bind(this), params);
    }

    onsendGetAiCfg(res:HttpReturn){
        if (res.res && res.res.code == 200){
            //GameData.aiCoin = res.res.data.consume;
            // if (this.argc.sendGetTaskRewardCallBack){
            //     this.argc.sendGetTaskRewardCallBack(res.res);
            //     this.argc.sendGetTaskRewardCallBack = null;
            // }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取邀请奖励日志*/
    sendGetInviteLog(success:Function = null){
        let params = {  
        }
        this.argc.sendGetInviteLogCallBack = success;
        zc.http.post("/inviteLog", this.onsendGetInviteLog.bind(this), params);
    }

    onsendGetInviteLog(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendGetInviteLogCallBack){
                this.argc.sendGetInviteLogCallBack(res.res);
                this.argc.sendGetInviteLogCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**获取邀请奖励详细信息*/
    sendGetInviteDetail(success:Function = null){
        let params = {  
        }
        this.argc.sendGetInviteDetailCallBack = success;
        zc.http.post("/inviteDetail", this.onsendGetInviteDetail.bind(this), params);
    }

    onsendGetInviteDetail(res:HttpReturn){
        if (res.res && res.res.code == 200){
            if (this.argc.sendGetInviteDetailCallBack){
                this.argc.sendGetInviteDetailCallBack(res.res);
                this.argc.sendGetInviteDetailCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }

    /**领取昨日收益奖励*/
    sendGetInviteReward(success:Function = null){
        let params = {  
        }
        this.argc.sendGetInviteRewardCallBack = success;
        zc.http.post("/inviteReceive", this.onsendGetInviteReward.bind(this), params);
    }

    onsendGetInviteReward(res:HttpReturn){
        if (res.res && res.res.code == 200){
            this.sendGetuserinfo();
            if (this.argc.sendGetInviteRewardCallBack){
                this.argc.sendGetInviteRewardCallBack(res.res);
                this.argc.sendGetInviteRewardCallBack = null;
            }
        }else{
            this.showFail("" , res.res? res.res.msg: res.err)
        }
    }



    refreshView(){
        EventManager.eventTarget.emit(EventManager.REFRESH_GAME);
    }

    showFail(str:string,  msg:string = "") {
        UIManager.showTip(msg);
    }

}

